package org.dizem.intellipainter.component.stroke;

import org.dizem.intellipainter.model.StrokeType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: dizem@126.com
 * Time: 12-2-19 下午12:08
 */
public class StrokeButton extends JToggleButton {
	private final StrokeType strokeType;
	private static final Dimension SIZE = new Dimension(60, 22);
	private static final Font font = new Font("微软雅黑", Font.PLAIN, 13);

	public StrokeButton(final StrokeType strokeType, final StrokeSelectCallback callback) {
		this.strokeType = strokeType;
		setDoubleBuffered(true);
		setMaximumSize(SIZE);
		setMinimumSize(SIZE);
		setPreferredSize(SIZE);

		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callback.onSelectStroke(strokeType);
			}
		});

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setFont(font);
		Graphics2D g2d = (Graphics2D) g;
		RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHints(qualityHints);
		g.setColor(Color.gray);
		int width = (int) strokeType.getStroke().getLineWidth();
		g.fillRect(5, (22 - width) / 2, 50, width);
	}

	public StrokeType getStrokeType() {
		return strokeType;
	}
}
